package cn.ynu.cmrs.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import cn.ynu.cmrs.R;
import cn.ynu.cmrs.MyApplication;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Activity displays the current location and classroom location
 * 
 * @author Yasha
 *
 */
public class GoogleMapActivity extends MapActivity {

	MapView mMapView;
	MapController mMapController;
	GeoPoint startPosition;
	GeoPoint endPosition;
	String classroomAddr = "";

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		MyApplication.allActivity.add(this);
		setContentView(R.layout.google_map);
		int startLatitude = getIntent().getIntExtra("startLatitude", 0);
		int startLongitude = getIntent().getIntExtra("startLongitude", 0);
		int endLatitude = getIntent().getIntExtra("endLatitude", 0);
		int endLongitude = getIntent().getIntExtra("endLongitude", 0);
		classroomAddr = getIntent().getStringExtra("classroomAddr");
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);
		Drawable drawable = getResources().getDrawable(android.R.drawable.ic_notification_overlay);
		MyItemizedOverlay _1stOverlay = new MyItemizedOverlay(drawable, this); 
		// Create a GeoPoint object indicates the user's current position
		//startPosition = new GeoPoint(startLatitude, startLongitude);
		// Create a GeoPoint object indicates the classroom's position
		endPosition = new GeoPoint(endLatitude, endLongitude);
		OverlayItem overlayItem01 = new OverlayItem(endPosition, getString(R.string.classroom_address),
				classroomAddr);
		/*OverlayItem overlayItem02 = new OverlayItem(startPosition, getString(R.string.current_position),
				classroomAddr);*/
		//Attach classroom's position on the map
		_1stOverlay.addOverlay(overlayItem01);
		//Attach the user's current position on the map
		//_1stOverlay.addOverlay(overlayItem02);
		List<Overlay> mapOverlays = mMapView.getOverlays();
		mapOverlays.add(_1stOverlay);
		mMapController = mMapView.getController();
		mMapController.setZoom(18);
		mMapController.animateTo(endPosition);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

		private ArrayList<OverlayItem> overlayItems = new ArrayList<OverlayItem>();
		private Context mContext;

		public MyItemizedOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		}

		public MyItemizedOverlay(Drawable defaultMarker, Context context) {
			super(boundCenterBottom(defaultMarker));
			this.mContext = context;
		}

		@Override
		protected OverlayItem createItem(int i) {
			// TODO Auto-generated method stub
			return overlayItems.get(i);
		}

		@Override
		public int size() {
			// TODO Auto-generated method stub
			return overlayItems.size();
		}

		// Used to added generated overlayItem object to the List
		public void addOverlay(OverlayItem overlayItem) {
			overlayItems.add(overlayItem);
			populate();
		}

		@Override
		protected boolean onTap(int index) {
			// TODO Auto-generated method stub
			OverlayItem item = overlayItems.get(index);
			AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
			builder.setTitle(item.getTitle());
			builder.setMessage(item.getSnippet());
			Dialog dialog = builder.create();
			dialog.setCanceledOnTouchOutside(true);
			dialog.show();
			return true;
		}
	}
}
